<?php
if( !defined('CORE') ) exit('Request Error!');
/**
 * 后台管理控制器
 *
 */
class ctl_index
{
    /**
     * 用户登录后的主页面
     */
    public function index()
    {
    	

        $t1 = microtime(true);
        $menu = preg_replace('/,$/', '', mod_admin_menu::parse_menu() );
        tpl::assign('menu',  $menu );
        tpl::assign('user', cls_access::$accctl->get_userinfos() );
        tpl::display('index.tpl');
        exit();
    }

    /**
     * 用户登录
     */
    public function login()
    {
        $accctl = cls_access::get_instance();
        $rs = 0;
        $errmsg = '';
        $gourl = req::item('gourl', '');

        if(req::item('username', '') != '' && req::item('password', '') != '')
        {
            //$validate = req::item('validate', '');
            //$vdimg = new cls_securimage();
            //if( empty($validate) || !$vdimg->check($validate) )
            //{
              //$errmsg = 'Error：请输入正确的验证码！';
            //}
            //else
            //{
            try
            {
                $rs = $accctl->check_user(req::item('username'), req::item('password'));
            }
            catch ( Exception $e )
            {
                $errmsg = 'Error：'.$e->getMessage();
            }
            if( $rs == 1 )
            {
                $jumpurl = empty($gourl) ? '?ct=index' : $gourl;
                cls_access::show_message ('成功登录', '成功登录，正在重定向你访问的页面', $jumpurl);
                exit();
            }

            // }
        }

        tpl::assign('gourl', $gourl );
        tpl::assign('errmsg', $errmsg );
        tpl::display('login.tpl');
        exit();
    }

    /**
     * 系统消息
     */
    public function adminmsg()
    {
        $addjob = req::item('addjob', '');
        if($addjob=='del')
        {
            db::query("Update `users_admin_log` set `isread`=1  where `isalert`=1 ");
            exit('ok');
        }
        else
        {
            $row = db::get_one("Select count(*) as dd From `users_admin_log` where `isalert`=1 And `isread`=0 ");
            if( is_array($row) && $row['dd']>0 )
            {
                exit($row['dd']);
            } else {
                exit('false');
            }
        }
    }

    /**
     * 退出
     */
    public function loginout()
    {
        $accctl = cls_access::get_instance();
        $accctl->loginout();
        cls_access::show_message ('注销登录', '成功退出登录！', '/?ct=index&ac=login');
        exit();
    }

    /**
     * 验证码图片, 必须此action要是公开权限
     */
    public function validate_image()
    {
        $vdimg = new cls_securimage(4, 150, 30, 3);
        $vdimg->show();
    }
}
